#   FSM template example implementation
#
#   Copyright (C) 2013 Dushara Jayasinghe.
#
#   fsm_template is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   fsm_template is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with fsm_template.  If not, see <http://www.gnu.org/licenses/>.

OBJ = calc.o main.o

define newline

endef

all: calc

calc:  $(OBJ)
	gcc -I. $(OBJ) -o calc

%.o: %.c
	gcc -I. -c $< -o $@

clean:
	@rm -rf *.o
	@rm -rf calc

# Basic set of unit tests
check: all test_begin_negated1_C 	\
	   test_ready_zero1				\
	   test_ready_int1				\
	   test_ready_frac1				\
	   test_negated1_zero1			\
	   test_negated1_int1			\
	   test_negated1_frac1			\
	   test_zero1_frac1				\
	   test_zero1_int1				\
	   test_zero1_CE				\
	   test_int1_frac1				\
	   test_int1_CE					\
	   test_frac1_CE				\
	   test_operand1_opEntered		\
	   test_opEntered_negated2		\
	   test_opEntered_zero2			\
	   test_opEntered_int2			\
	   test_opEntered_frac2			\
	   test_negated2_opEntered		\
	   test_negated2_zero2			\
	   test_negated2_int2			\
	   test_negated2_frac2			\
	   test_zero2_frac2				\
	   test_zero2_int2				\
	   test_zero2_opEntered			\
	   test_zero2_divide			\
	   test_zero2_equals			\
	   test_int2_frac2				\
	   test_int2_equals2			\
	   test_frac2_opEntered			\
	   test_frac2_equals			\
	   test_int2_opEntered			\
	   test_result_subtract			\
	   test_result_add				\
	   test_begin_divide

test_begin_negated1_C:
	@echo $@
	@echo begin:subtract_op > expect.tmp
	@echo negated1:CE >> expect.tmp
	@./compare_output.sh -d expect.tmp
	@rm expect.tmp

test_ready_zero1:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@./compare_output.sh 0 expect.tmp
	@rm expect.tmp
	
test_ready_int1:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 1 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 2 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 3 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 4 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 5 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 6 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 7 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 8 expect.tmp

	@echo ready:DIGIT_1_9 > expect.tmp
	@./compare_output.sh 9 expect.tmp
	@rm expect.tmp
	
test_ready_frac1:
	@echo $@
	@echo ready:POINT > expect.tmp
	@./compare_output.sh . expect.tmp
	@rm expect.tmp
	
test_negated1_zero1:
	@echo $@
	@echo begin:subtract_op > expect.tmp
	@echo negated1:DIGIT_0 >> expect.tmp
	@./compare_output.sh -0 expect.tmp
	@rm expect.tmp

test_negated1_int1:
	@echo $@
	@echo begin:subtract_op > expect.tmp
	@echo negated1:DIGIT_1_9 >> expect.tmp
	@./compare_output.sh -1 expect.tmp
	@rm expect.tmp

test_negated1_frac1:
	@echo $@
	@echo begin:subtract_op > expect.tmp
	@echo negated1:POINT >> expect.tmp
	@./compare_output.sh -. expect.tmp
	@rm expect.tmp

test_zero1_frac1:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:POINT >> expect.tmp
	@./compare_output.sh 0. expect.tmp
	@rm expect.tmp

test_zero1_int1:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:DIGIT_1_9 >> expect.tmp
	@./compare_output.sh 02 expect.tmp
	@rm expect.tmp

test_zero1_CE:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@echo operand1:CE >> expect.tmp
	@./compare_output.sh 0d expect.tmp
	@rm expect.tmp

test_int1_frac1:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:DIGIT_1_9 >> expect.tmp
	@echo int1:POINT >> expect.tmp
	@./compare_output.sh 02. expect.tmp
	@rm expect.tmp

test_int1_CE:
	@echo $@
	@echo ready:DIGIT_1_9 >> expect.tmp
	@echo operand1:CE >> expect.tmp
	@./compare_output.sh 2d expect.tmp
	@rm expect.tmp

test_frac1_CE:
	@echo $@
	@echo ready:POINT > expect.tmp
	@echo operand1:CE >> expect.tmp
	@./compare_output.sh .d expect.tmp
	@rm expect.tmp
	
test_operand1_opEntered:
	@echo $@
	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:DIGIT_1_9 >> expect.tmp
	@echo int1:POINT >> expect.tmp
	@echo operand1:add_op >> expect.tmp
	@./compare_output.sh 02.+ expect.tmp
	
	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:DIGIT_1_9 >> expect.tmp
	@echo int1:POINT >> expect.tmp
	@echo operand1:subtract_op >> expect.tmp
	@./compare_output.sh 02.- expect.tmp

	@echo ready:DIGIT_0 > expect.tmp
	@echo zero1:DIGIT_1_9 >> expect.tmp
	@echo int1:POINT >> expect.tmp
	@echo operand1:divide_op >> expect.tmp
	@./compare_output.sh 02./ expect.tmp

	@rm expect.tmp

test_opEntered_negated2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:subtract_op >> expect.tmp
	@./compare_output.sh 5+- expect.tmp

test_opEntered_zero2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@./compare_output.sh 5+0 expect.tmp

test_opEntered_int2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_1_9 >> expect.tmp
	@./compare_output.sh 5+4 expect.tmp

test_opEntered_frac2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:POINT >> expect.tmp
	@./compare_output.sh 2+. expect.tmp

test_negated2_opEntered:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:subtract_op >> expect.tmp
	@echo negated2:CE >> expect.tmp
	@./compare_output.sh 5+-d expect.tmp

test_negated2_zero2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:subtract_op >> expect.tmp
	@echo negated2:DIGIT_0 >> expect.tmp
	@./compare_output.sh 5+-0 expect.tmp

test_negated2_int2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:subtract_op >> expect.tmp
	@echo negated2:DIGIT_1_9 >> expect.tmp
	@./compare_output.sh 5+-9 expect.tmp

test_negated2_frac2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:subtract_op >> expect.tmp
	@echo negated2:POINT >> expect.tmp
	@./compare_output.sh 5+-. expect.tmp

test_zero2_frac2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo zero2:POINT >> expect.tmp
	@./compare_output.sh 5+0. expect.tmp

test_zero2_int2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo zero2:DIGIT_1_9 >> expect.tmp
	@./compare_output.sh 5+07 expect.tmp

test_zero2_opEntered:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo operand2:CE >> expect.tmp
	@./compare_output.sh 5+0d expect.tmp
	
test_zero2_divide:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo operand2:divide_op >> expect.tmp
	@./compare_output.sh 5+0/ expect.tmp
	
test_zero2_equals:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo operand2:EQUALS >> expect.tmp
	@./compare_output.sh 5+0= expect.tmp
	
test_int2_frac2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_1_9 >> expect.tmp
	@echo int2:POINT >> expect.tmp
	@./compare_output.sh 5+4. expect.tmp

test_int2_equals2:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_1_9 >> expect.tmp
	@echo operand2:EQUALS >> expect.tmp
	@./compare_output.sh 5+4= expect.tmp

test_int2_opEntered:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_1_9 >> expect.tmp
	@echo operand2:CE >> expect.tmp
	@./compare_output.sh 5+4d expect.tmp

test_frac2_opEntered:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:POINT >> expect.tmp
	@echo operand2:CE >> expect.tmp
	@./compare_output.sh 2+.d expect.tmp

test_frac2_equals:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:POINT >> expect.tmp
	@echo operand2:EQUALS >> expect.tmp
	@./compare_output.sh 2+.= expect.tmp

test_result_subtract:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo operand2:EQUALS >> expect.tmp
	@echo result:subtract_op >> expect.tmp
	@./compare_output.sh 5+0=- expect.tmp

test_result_add:
	@echo $@
	@echo ready:DIGIT_1_9 > expect.tmp
	@echo operand1:add_op >> expect.tmp
	@echo opEntered:DIGIT_0 >> expect.tmp
	@echo operand2:EQUALS >> expect.tmp
	@echo ready:add_op >> expect.tmp
	@./compare_output.sh 5+0=+ expect.tmp

test_begin_divide:
	@echo $@
	@echo ready:divide_op > expect.tmp
	@./compare_output.sh / expect.tmp
